package com.linked.goods.goodsbase.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.goods.goodsbase.bean.dto.GoodsBrandDTO;
import com.linked.goods.goodsbase.bean.po.GoodsBrandPO;
import com.linked.goods.goodsbase.bean.po.GoodsInfoPO;
import com.linked.goods.goodsbase.service.IGoodsBrandService;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :dbq
 * @date : 2023/5/17 8:59
 * @description : desc
 */
@RestController
@RequestMapping("/goodsbrand")
@Slf4j
public class GoodsBrandController {

    private final ObjectMapper mapper;

    private final IGoodsBrandService goodsBrandService;

    public GoodsBrandController(ObjectMapper mapper, IGoodsBrandService goodsBrandService) {
        this.mapper = mapper;
        this.goodsBrandService = goodsBrandService;
    }

        @PostMapping("saveGoodsBrand")
    Result saveGoodsBrand(@RequestBody GoodsBrandDTO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("保存品牌接口 入参：{}", mapper.writeValueAsString(param));
        }
        String brandId = goodsBrandService.saveGoodsBrand(new GoodsBrandPO(param));
        return !StringUtils.isEmpty(brandId) ? Result.success(brandId) : Result.ok(false, "保存失败！");
    }

    @PostMapping("queryAllBrandList")
    Result queryAllBrandList(@RequestBody GoodsBrandDTO param) {

        List<GoodsBrandDTO> brandList = null;
        try {
            brandList = goodsBrandService.queryAllBrandList(param);
        } catch (Exception e) {
            log.error("查询所有品牌接口 异常！", e);
            return Result.error("未知异常，请联系管理员！");
        }
        return !CollectionUtils.isEmpty(brandList) ? Result.success(brandList) : Result.ok(false, "未查询到数据！");
    }

}
